<?php
class Panel extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library(array(
                'form_validation', 
                'session',
                'uri'
            ));
        $this->load->model(array(
                'panel_model',
                'news_model',
                'events_model',
                'members_directory_model',
                'team_model'
            ));
        $this->load->helper(array(
                'url'
            ));
    }
    
    public function index() {
        redirect('/panel/login', 'refresh');
    }

    public function update($seccion = null) {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/panel/login/', 'refresh');
        }
        
        $idcontenido = array();
        if($seccion != null) {
            $idcontenido = $this->panel_model->get_id_contenido($seccion);
            if(!empty($idcontenido)) {
                $this->form_validation->set_rules('titulo', 'Titulo', 'required');
                if($this->form_validation->run() == FALSE) {
                    
                } else {
                    
                    $config['upload_path'] = './upload';
                    $config['allowed_types'] = 'gif|jpg|png|pdf';
            
                    $this->load->library('upload', $config);
                    $nombre_del_campo = 'foto';
                    $adjunto = null;
                    if(!$this->upload->do_upload($nombre_del_campo)) {
                        $error = array('error' => $this->upload->display_errors());
                    } else {
                        $adjunto = array('upload_data' => $this->upload->data());
                    }
                    
                    
                    $datos = array(
                        'titulo' => $this->input->post('titulo'),
                        'texto' => $this->input->post('texto')
                    );
                    if($adjunto != null) {
                        $datos['adjunto'] = '/upload/'.$adjunto['upload_data']['raw_name'].$adjunto['upload_data']['file_ext'];
                    }
                    
                    $this->panel_model->update($datos, $idcontenido['idcontenido']);
                    
                }
            }
        }
        
        
        if($seccion == null) {
            $left['seccion'] = 'who-we-are';
        } else {
            $left['seccion'] = $seccion;
        }
        
        if(!empty($idcontenido)) {
            $datos['contenido'] = $this->panel_model->get_contenido_por_parametros($left['seccion']);
        } else {
            $datos['contenido']['titulo'] = '';
            $datos['contenido']['texto'] = '';
        }
        
        $data['segmento'] = $this->uri->segment(3);
        $data['seccion'] = $left['seccion'];
        
        $t = explode('-', $seccion);
        $titulo = '';
        foreach ($t as $value) {
            $titulo .= strtoupper($value).' ';
        }
        $data['titulo'] = $titulo;
        
        $this->load->view('panel/header', $session);
        switch ($seccion) {
            case 'overview-what-we-do':
            case 'agribusiness':
            case 'private-capital-advisory':
            case 'real-estate':
            case 'welth-managment':
            case 'what-we-do':
            case 'investment-banking':
            case 'info':
            case 'our-services':
            case 'investment-management':
            case 'information-management':
            case 'trust-legal-service':
            case 'lifestyle-management':
                $this->load->view('panel/submenu-what-we-do');
                break;
            case 'who-we-are':
            case 'overview-who-we-are':
            case 'advisory-board':
            case 'team':
                $data['team'] = $this->team_model->gets('team');
                $data['advisory'] = $this->team_model->gets('advisory-board');
                $this->load->view('panel/submenu-who-we-are', $data);
                break;
            case 'why-we-do-it':
            case 'overview-why-we-do-it':
            case 'guiding-principles':
            case 'merchant-bank':
                $this->load->view('panel/submenu-why-we-do-it');
                break;
            case 'investors':
                break;
        }

        $this->load->view('panel/contenido', $datos);
    }
    
    public function login() {
        $session = $this->session->all_userdata();
        
        $this->form_validation->set_rules('user', 'Usuario', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $usuario = $this->panel_model->login($this->input->post('user'), $this->input->post('pass'));
            if(!empty($usuario)) {
                $datos = array(
                    'SID' => $usuario['idusuario'],
                    'usuario' => $usuario['usuario'],
                    'idtipodeusuario' => $usuario['idtipodeusuario'],
                    'nombre' => $usuario['nombre'],
                    'apellido' => $usuario['apellido']
                );
                $this->session->set_userdata($datos);
                $session = $this->session->all_userdata();//redirect('/home/', 'refresh');
                
            }
         
        }
        
        if(!empty($session['SID'])) {
            redirect('/panel/update/who-we-are/', 'refresh');
        } else {
            $this->load->view('panel/login');
        }
    }
    
    public function logout() {
        $this->session->sess_destroy();
        redirect('/panel/login/', 'refresh');
    } 
    
    public function news($seccion) {
        if($seccion == null) {
            $seccion = 'news';
        }
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/panel/login/', 'refresh');
        }
        
        $this->form_validation->set_rules('fecha', 'Fecha', 'required');
        $this->form_validation->set_rules('titulo', 'Título', 'required');
        if($seccion == 'knightsbridge-watch') {
            $this->form_validation->set_rules('parrafo', 'Parrafo', 'required');
        }
        $this->form_validation->set_rules('resumen', 'Resumen', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $session = $this->session->all_userdata();
            
            $datos = array(
                'fecha' => $this->input->post('fecha'),
                'titulo' => $this->input->post('titulo'),
                'parrafo' => $this->input->post('parrafo'),
                'resumen' => $this->input->post('resumen'),
                'seccion' => $seccion
            );

            if($seccion == 'market-news') {
                $config['upload_path'] = "./upload/";
                $config['allowed_types'] = "pdf";
                $config['max_size'] = "2048";
                $config['encrypt_name'] = false;
                $config['remove_spaces'] = true;

                $this->load->library('upload', $config);
                
                if(!$this->upload->do_upload('pdf')) {
                    $error = array('error' => $this->upload->display_errors());
                } else {
                    $data = array('upload_data' => $this->upload->data());
                    $datos['pdf'] = $data['upload_data']['file_name'];
                }
            }
            
            $this->news_model->set_news($datos);

            
        }
        
        $data['seccion'] = $seccion;
        $data['news'] = $this->news_model->get_news($seccion);
        
        
        $this->load->view('panel/header', $session);
        $this->load->view('panel/submenu-news');
        $this->load->view('panel/news', $data);
    }
    
    public function events($idioma = null) {
        if($idioma == null) {
            $idioma = 'en';
        }
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/panel/login/', 'refresh');
        }
        $left['idioma'] = $idioma;
        
        $data['idioma'] = $idioma;
        
        $this->form_validation->set_rules('fecha', 'Fecha', 'required');
        $this->form_validation->set_rules('descripcion', 'Descripcion', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $fecha = substr($this->input->post('fecha'), 6, 4);
            $fecha .= '-';
            $fecha .= substr($this->input->post('fecha'), 0, 2);
            $fecha .= '-';
            $fecha .= substr($this->input->post('fecha'), 3, 2);
            $fecha .= substr($this->input->post('fecha'), 10, 9);
            
            $datos = array(
                'fecha' => $fecha,
                'descripcion' => $this->input->post('descripcion'),
                'idioma' => $idioma
            );
            
            $this->events_model->set_events($datos);
        }
        
        $data['segmento'] = $this->uri->segment(2);
        $data['events'] = $this->events_model->get_events($idioma);
        switch ($left['idioma']) {
            case 'ar':
                $data['idiomat'] = 'Arabic';
                break;
            case 'en':
                $data['idiomat'] = 'English';
                break;
            case 'es':
                $data['idiomat'] = 'Spanish';
                break;
            case 'pt':
                $data['idiomat'] = 'Portuguese';
                break;
        }
        
        $this->load->view('panel/header', $session);
        $this->load->view('panel/leftsidebar', $left);
        $this->load->view('panel/events', $data);
    }
    
    public function delete($seccion, $id) {
        switch ($seccion) {
            case 'news':
            case 'market-news':
            case 'knightsbridge-watch':
                $this->news_model->delete_news($id);
                redirect('/panel/news/'.$seccion.'/', 'refresh');
                break;
        }
        redirect('/panel/update/'.$seccion.'/', 'refresh');
    }
    
    public function members($idioma = null) {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/panel/login/', 'refresh');
        }
        
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'idioma' => $idioma
            );
            
            $this->members_directory_model->set_members_directory($datos);
            
        }
        
        
        
        $left['idioma'] = $idioma;
        
        $data['idioma'] = $idioma;
        
        if($idioma == null) {
            $left['idioma'] = 'en';
        } else {
            $left['idioma'] = $idioma;
        }
        $seccion = 'members-directory';
        $left['seccion'] = $seccion;
        
        $data['segmento'] = $this->uri->segment(3);
        $data['seccion'] = $left['seccion'];
        switch ($left['idioma']) {
            case 'ar':
                $data['idioma'] = 'Arabic';
                break;
            case 'en':
                $data['idioma'] = 'English';
                break;
            case 'es':
                $data['idioma'] = 'Spanish';
                break;
            case 'pt':
                $data['idioma'] = 'Portuguese';
                break;
        }
        
        $t = explode('-', $seccion);
        $titulo = '';
        foreach ($t as $value) {
            $titulo .= strtoupper($value).' ';
        }
        $data['titulo'] = $titulo;
        
        $data['members'] = $this->members_directory_model->get_members_directory($idioma);
        
        $this->load->view('panel/header', $session);
        $this->load->view('panel/leftsidebar', $left);
        $this->load->view('panel/members', $data);
        
    }
    
    public function team($idteam) {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/panel/login/', 'refresh');
        }
        
        $this->form_validation->set_rules('nombre', 'Nombre', 'required');
        $this->form_validation->set_rules('texto', 'Texto', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'nombre' => $this->input->post('nombre'),
                'texto' => $this->input->post('texto')
            );
            
            $this->team_model->update($datos, $idteam);
        }
        
        $data['team'] = $this->team_model->gets('team');
        $data['advisory'] = $this->team_model->gets('advisory-board');
        $data['contenido'] = $this->team_model->get($idteam);        
                
        $this->load->view('panel/header', $session);
        $this->load->view('panel/submenu-who-we-are', $data);
        $this->load->view('panel/team');
    }
}
?>
